<?php

namespace Home\Controller;
use Think\Controller; 
class AddressController extends Controller {
	
	public function __construct() {
		parent::__construct();
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
	public function index(){
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$province=empty($_REQUEST['province'])?'':trim($_REQUEST['province']);
		$city=empty($_REQUEST['city'])?'':trim($_REQUEST['city']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		$id=empty($_REQUEST['id'])?'':trim($_REQUEST['id']);
		$visitor_id=empty($_REQUEST['visitor_id'])?'':trim($_REQUEST['visitor_id']);
		
		$address =D('address'); 
		$result=$address->address_list($pageNo,$pageSize,$name,$asc,$province,$city,$id,$visitor_id);
		
		$this->assign("visitor_id",$visitor_id);
		$this->assign("address_list",$result['address_list']);
		$this->assign("count",$result['count']);
		$this->assign("data",$result['data']);
		$this->display();
    }
	
	
	function address_info()
	{  
		$data['province']	    =	empty($_POST['province'])?'':trim($_POST['province']);
		$data['city']	    =	empty($_POST['city'])?'':trim($_POST['city']);
		$data['district']   	=	empty($_POST['district'])?'':trim($_POST['district']);
		$data['town']	    =	empty($_POST['town'])?'':trim($_POST['town']);
		$data['address']	    =	empty($_POST['address'])?'':trim($_POST['address']);
		$data['contact']	=	empty($_POST['contact'])?'':trim($_POST['contact']);
		$data['phone']	    =	empty($_POST['phone'])?'':trim($_POST['phone']);
		$data['is_default']	    =	empty($_POST['is_default'])?'':trim($_POST['is_default']);
		$data['modify_time']   	=	date('Y-m-d H:i:s');
		
		//chen 2016-09-20
		$data['visitor_id']	=	$_SESSION['user']['id'];
		$visitor_id = $_SESSION['user']['id'];
		$id=empty($_POST['id'])?0:$_POST['id'];
		
		$address=M('address');
		if(empty($id))
		{   
		    $data['add_time']   	=	date('Y-m-d H:i:s');
			$res=$address->add($data);
			if($res)
			{
				if($data['is_default']==1)
				{
					$update['is_default']=0;
					$update['visitor_id']	=	$_SESSION['user']['id'];
					
					$reu=$address->where("id!='$res' and visitor_id = '$visitor_id' ")->save($update);
				}
			}
			header('Location:index');
		}
		else
		{
			$res=$address->where("id='$id' and visitor_id = '$visitor_id'")->save($data);
			
			header('Location:'.$_SERVER['HTTP_REFERER']);
		}
	}
	
	/* 地址 双击修改*/
	public function address_edit() 
	{
		
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$name=empty($_REQUEST['name'])?'':$_REQUEST['name'];
		$value=empty($_REQUEST['value'])?'':$_REQUEST['value'];
		$visitor_id=empty($_REQUEST['visitor_id'])?$_SESSION['user']['id']:$_REQUEST['visitor_id'];
	
		$data[$name]=$value;
		$address=M('address');
		
		$res=$address->where("id='$id' and visitor_id = '$visitor_id'")->save($data);
		dump($res);die;
		if($res!==false)
		{   
		   /* if($name=='is_default' && $value=1)
			{
				$update['is_default']=0;
				$update['visitor_id']	=	$_SESSION['user']['id'];
				$reu=$address->where("id='$id' and visitor_id = '$visitor_id'")->save($update);
			}*/
			$msg='success';
		}
		else
		{
			$msg='fail';
		}
		
		echo $msg;
	}
	
	
	//
	public function address_delete(){
		$visitor_id = $_SESSION['user']['id'];  
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		
		$address=M('address');
		$res=$address->where("id='$id' and visitor_id = '$visitor_id'")->delete();
		
		header("Location:".$_SERVER['HTTP_REFERER']);
    }
	
 	
}
















